function [P_smoothed filter_name]=apply_filters(P_raw, filter_win_sizes)
% low-pass filter the periodogram with a list of filter width

% P_raw is the unsmoothed periodogram
% filter_win_sizes is a vector of ints e.g. [2 3 4 6 8 10 15] 

for c = 1:numel(filter_win_sizes) % go through the vector (list) of filter widths
       
    
    %*************** EXTEND PERIODOGRAM BEYOND FREQUENCIES 0 AND 0.5 BEFORE SMOOTHING
    %  Before smoothing periodogram, want to extend it so that it is 
    % symmetric about frequencies 0 and 0.5  /yr
    %n=length(y);		% first element holds no ordinates in half-pdgm
    n = length(P_raw); % number of "bins" in raw pgram
    yrev = P_raw(n:-1:1); %reversed version of P_raw
    yext = [yrev;P_raw;yrev];  % this is a triplicate version of pgram  
    
    % A symmetric series of length n has been tacked on each end of the pdgm.
    % So the original periodogram really covers the elements n+1 through 
    % 2n of yext.  And, when convolute with an m-weight Daniell filter, will
    % have effect of shifting (m-1)/2 units relative to central weight.  So will
    % eventually want to plot and analyze this range of yext as the smoothed 
    % periodogram:   n+1+(m-1)/2  thru  2n+(m-1)/2
    % CH: I think the point is that convoluting will "eat" away a part of
    % the data (pgram) from left and right (see his chapter 8. fig. 82), so
    % the triplication protects agains that
    
    
    % CH: Perron's fft1d.m uses a rectangular filter with filter() in log space 
    % - which seems to move the peaks to the right. I substituded this with
    % low pass filtering from David Meko's Pdgm5.m, which uses conv(). This doesn't do
    % the log transform and filters a "triplicate" pgram, maybe that's what
    % keeps the peeks in place (?)
    %P_sm =10.^filter(win,1,log10(P_raw)); % smooth in log space (why in log?)
    
    mm = filter_win_sizes(c); % current filter width from looped list 
    
    rwin = ones(1,mm)/mm; % recangular window 
    hnwin = hann(mm); % hann(ing) window
    bwin = wtsbinom(mm); % binomial
    hmwin = hamming(mm); % hamming
    
    % daniell window (see David Mako's danwgt1.m)
    dwin = zeros(mm,1); 
    wt = 1 / (mm-1); % weights are 1 except for the two end weights
    dwin=[ wt/2; wt(ones(mm-2,1),:); wt/mm];
    
    % which window to use?
    win = bwin; % win = dwin or: hwin or rwin, etc.
    filter_name = 'binom'; % just a label for plotting
    
    ysm=conv(yext,win); % low pass filter the "triple" pgram with window
    
    % cut filtered triple pgram back to single
    igo = n+1+(mm-1)/2;  % start
    istop=2*n+(mm-1)/2;  % end   
    P_sm = ysm(igo:istop);  
        
    P_smoothed(:, c) = P_sm; % add to collection
    
   
end
